package com.next.aap.server.persistance.helper;

import java.util.List;

import org.springframework.stereotype.Component;

import com.next.aap.server.persistance.Manifesto;
import com.next.core.db.page.PageInfo;
import com.next.core.db.page.PageResult;
import com.next.core.db.page.QueryParam;
import com.next.core.db.validator.ObjectValidator;
import com.next.core.exception.AppException;
import com.next.ext.core.db.page.HibernateQueryParamPageInfo;
import com.next.ext.core.persistance.dao.BasePersistanceDao;

@Component
public class ManifestoHelper extends BasePersistanceDao<Manifesto> implements ObjectValidator<Manifesto> {

	/**
	 * Creates/updates a manifesto in Database
	 * 
	 * @param manifesto
	 * @return saved manifesto
	 * @throws AppException
	 */
	public Manifesto saveManifesto(Manifesto manifesto) throws AppException
	{
		manifesto = super.saveObject(manifesto);
		return manifesto;
	}

	/**
	 * deletes a manifesto in Database
	 * 
	 * @param manifesto
	 * @return updated manifesto
	 * @throws AppException
	 */
	public void deleteManifesto(Manifesto manifesto) throws AppException {
		super.deleteObject(manifesto);
	}

	/**
	 * return a Manifesto with given primary key/id
	 * 
	 * @param id
	 * @return Manifesto with PK as id(parameter)
	 * @throws AppException
	 */
	public Manifesto getManifestoById(Long id) throws AppException
	{
		Manifesto manifesto = super.getObjectByPK(Manifesto.class, id);
		return manifesto;
	}

	/**
	 * @param pageInfo
	 * @return search result
	 * @throws AppException
	 */
	public PageResult<Manifesto> searchManifestos(PageInfo pageInfo) throws AppException
	{
		return super.findObject(Manifesto.class, pageInfo);
	}

	public List<Manifesto> getAllManifestos() throws AppException
	{
		PageInfo pageInfo = null;
		return super.findObject(Manifesto.class, pageInfo).getResultList();
	}
	public Manifesto getManifestoOfAssemblyConstituency(long assemblyConstituencyId) throws AppException{
		HibernateQueryParamPageInfo pageInfo = new HibernateQueryParamPageInfo();
		QueryParam stateQueryParam = new QueryParam();
		stateQueryParam.setCaseSenstive(false);
		stateQueryParam.setField("assemblyConstituencyId");
		stateQueryParam.setFieldType(QueryParam.FIELD_TYPE_NUMBER);
		stateQueryParam.setOperator(QueryParam.OPERATOR_EQUAL);
		stateQueryParam.setValue(assemblyConstituencyId);
		pageInfo.addCriteria(stateQueryParam);
		
		Manifesto manifesto = this.searchOneObject(Manifesto.class,pageInfo);
		return manifesto;
	}

	public void validateObjectForCreate(Manifesto manifesto) throws AppException {
		checkIfObjectMissing("AssemblyConstituency", manifesto.getAssemblyConstituency());
	}

	public void validateObjectForUpdate(Manifesto manifesto) throws AppException {
		checkIfObjectMissing("AssemblyConstituency", manifesto.getAssemblyConstituency());
	}
	
}